package monopoly.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import monopoly.GameState;



public class BoardGraphics extends Component {

	private static final long serialVersionUID = 1L;
	private BufferedImage img;
	private GameState gameState;
	private Graphics board;

	
	public void paint(Graphics board)
	{
		board.setColor(Color.white);
		board.fillRect(0,0,1000,700);
		
		GameBoardSpace[] gameBoardSpaceArray = gameState.getGameBoard().getSpaceArray();
		
		for ( int ii=0 ; ii <= 40 ; ii++ )
		{
			img = null;
			GameBoardSpace gameBoardSpace = gameBoardSpaceArray[ii];
			String filename = gameBoardSpace.getBoardSpaceFN();
			int xPos = gameBoardSpace.getXPosition();
			int yPos = gameBoardSpace.getYPosition();
			if (ii != 40)		// "Jail" is already drawn.  No need to redraw it.
			{
				try {
					img = ImageIO.read(new File(filename));
				}	catch (IOException e) {}
				board.drawImage(img,xPos,yPos,null);
			}
				
			if (gameState.getActivePlayer().getBoardPosition() == ii)
			{
				try {
					img = ImageIO.read(new File (gameState.getActivePlayer().getToken().getImageFN()));
				}	catch (IOException e) {}
				board.drawImage(img,gameBoardSpace.getXTokenPosition(), gameBoardSpace.getYTokenPosition(), null);
				
				try {
					img = ImageIO.read(new File (gameBoardSpaceArray[ii].getCardFN()));
				}	catch (IOException e) {}
				board.drawImage(img, 250, 200, null);
			}
			if (gameState.getInactivePlayer().getBoardPosition() == ii)
			{
				try {
					img = ImageIO.read(new File (gameState.getInactivePlayer().getToken().getImageFN()));
				}	catch (IOException e) {}
				board.drawImage(img,gameBoardSpace.getXTokenPosition(), gameBoardSpace.getYTokenPosition(), null);
			}
		}

		//outline the board
		board.setColor(Color.black);
		board.drawRect(80,80,540,540);
		board.drawRect(0,0,700,700);

		//Monopoly logo
		img = null;
		try {
			img = ImageIO.read(new File("images/logo.gif"));
		} catch (IOException e) {}
		board.drawImage(img ,200, 100, null);
		
		//Dice
		img = null;
		try {
			img = ImageIO.read(new File(gameState.getDie1().getFN()));
		}	catch (IOException e) {}
		board.drawImage(img, 300, 540, null);
		
		img = null;
		try {
			img = ImageIO.read(new File(gameState.getDie2().getFN()));
		}	catch (IOException e) {}
		board.drawImage(img, 370, 540, null);
		
		this.board = board;
		
	}
	
	public BoardGraphics()
	{
		this.img = null;
		this.gameState = null;
	}
	
	public BoardGraphics(GameState gameState)
	{
		img = null;
		this.gameState = gameState;	
	}
	
	public void setGameState(GameState gameState)
	{
		this.gameState = gameState;
		repaint();		
	}
	
	public Dimension getPreferredSize() 
	{
		return new Dimension(701, 701);
	}
	
	public Graphics getBoard()
	{
		return board;
	}
}
